<?php
/**
 * Deco controller
 */
namespace App\Http\ControllersAdmin;

use App\Service\DecoService;
use App\Validation\DecoValidation;
use App\Validation\DecoSearchValidation;
use App\Validation\DeleteValidation;
use App\Validation\BatchValidation;

class DecoController extends BaseController
{
	/**
	 * [indexAction 列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{
		$this->getUser();
		$raw = $this->validation(new DecoSearchValidation, $this->request->getJsonRawBody());

		$data = DecoService::getSearch($raw);	
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->num  = (string)count(json_decode($value->pics));//图片数量
			// unset($d[$key]->pics);
			$d[$key]->pics = json_decode($value->pics);
		}	
		$count = DecoService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [recoAction 最新推荐列表]
	 * @return [type] [description]
	 */
	public function bannerAction()
	{
		$this->getUser();
		$raw = $this->validation(new DecoSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$raw->reco_by = 2;
		$data = DecoService::getSearch($raw);
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->num  = (string)count(json_decode($value->pics));//图片数量
			// unset($d[$key]->pics);
			$d[$key]->pics = json_decode($value->pics);
		}		
		$count = DecoService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [firstAction 首页列表]
	 * @return [type] [description]
	 */
	public function firstAction()
	{
		$this->getUser();
		$raw = $this->validation(new DecoSearchValidation, $this->request->getJsonRawBody());
		// $raw->city_id = $this->city_id;
		$raw->index_by = 2;
		$data = DecoService::getSearch($raw);	
		$d = [];
		foreach ($data as $key => $value) {
			$d[$key] = $value;
			$d[$key]->num  = (string)count(json_decode($value->pics));//图片数量
			// unset($d[$key]->pics);
			$d[$key]->pics = json_decode($value->pics);
		}
		$count = DecoService::getSearch($raw, TRUE);

		$assign = [
			'total' => $count,
			'list'  => $d
		];
		return $this->echoJson($assign);
	}

	/**
	 * [createAction 创建]
	 * @return [type] [description]
	 */
	public function createAction()
	{
		$this->getUser();
		$raw = $this->validation(new DecoValidation, $this->request->getJsonRawBody());
		$data = DecoService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [stickAction 置顶]
	 * @return [type] [description]
	 */
	public function stickAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		DecoService::getStick($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [updateAction 更新]
	 * @return [type] [description]
	 */
	public function updateAction()
	{
		$this->getUser();
		$raw = $this->validation(new DecoValidation, $this->request->getJsonRawBody());
		$raw->pics = json_encode($raw->pics);
		$data = DecoService::save($raw);
		return $this->echoJson('ok');
	}

	/**
	 * [detailAction 详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser();
		$id = $this->dispatcher->getParam('id');
		$data = DecoService::getModel($id);
		$data->pics = json_decode($data->pics);
		return $this->echoJson($data);
	}

	/**
	 * [setrecoAction 执行推荐  将video数据列表推荐到首页-banner]
	 * @return [type] [description]
	 */
	public function setrecoAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		DecoService::batchReco($raw->id, 2);
		return $this->echoJson("ok");
	}

	/**
	 * [delrecoAction 执行取消推荐]
	 * @return [type] [description]
	 */
	public function delrecoAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		DecoService::batchReco($raw->id);
		return $this->echoJson("ok");
	}

	/**
	 * [setrecoAction 执行首页 将video数据列表推荐到首页-video]
	 * @return [type] [description]
	 */
	public function setfirstAction()
	{
		$raw = $this->validation(new BatchValidation, $this->request->getJsonRawBody());
		DecoService::batchReco($raw->id, 1);
		return $this->echoJson("ok");
	}

	/**
	 * [upAction 上架]
	 * @return [type] [description]
	 */
	public function displayAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = DecoService::display($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [deleteAction 删除]
	 * @return [type] [description]
	 */
	public function deleteAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = DecoService::delete($raw->id);
		return $this->echoJson('ok');
	}

	/**
	 * [foreverAction 彻底删除]
	 * @return [type] [description]
	 */
	public function foreverAction()
	{
		$this->getUser();
		$raw = $this->validation(new DeleteValidation, $this->request->getJsonRawBody());
		$data = DecoService::forever($raw->id);
		return $this->echoJson('ok');
	}
}